<template>
    <div class="app-container">
        <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
            <el-form-item label="页面名称" prop="pageName">
                <el-input v-model="queryParams.pageName" clearable placeholder="请输入页面名称" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-checkbox v-model="isType" style="margin-left: 20px">{{ isType ? '列表' : '卡片' }}</el-checkbox>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button v-hasPermi="['ghxx:bEchartZt:add']" icon="el-icon-plus" plain size="mini" type="primary" @click="handleAdd">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :border="false" v-if="isType" v-loading="loading" :data="bEchartZtList" @selection-change="handleSelectionChange">
            <el-table-column align="center" type="selection" width="55" />
            <el-table-column align="center" label="页面名称" prop="pageName" />
            <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                <template slot-scope="scope">
                    <el-button icon="el-icon-edit" size="mini" type="text" @click="handleClickZt(scope.row)">编辑</el-button>
                    <el-button icon="el-icon-view" size="mini" type="text" @click="handlePreview(scope.row)">预览</el-button>
                    <el-button v-hasPermi="['ghxx:bEchartZt:remove']" icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-row v-else v-loading="loading">
            <el-col v-for="item in bEchartZtList" :key="item.id" :span="4" style="padding: 2px">
                <div style="width: auto; height: 300px">
                    <el-card :body-style="{ padding: '5px' }">
                        <span class="device-name">{{ item.pageName }}</span>
                        <div @click="handlePreview(item)">
                            <el-image :src="item.pageImage == null ? noImage : baseApi + item.pageImage" class="ztImage" title="编辑" />
                        </div>
                        <div style="padding: 5px">
                            <span>{{ item.pageName }}</span>
                            <div class="bottom clearfix">
                                <el-button icon="el-icon-edit" type="text" @click="handleClickZt(item)">编辑</el-button>
                                <el-button icon="el-icon-view" type="text" @click="handlePreview(item)">预览</el-button>
                                <el-button icon="el-icon-delete" type="text" @click="handleDelete(item)">删除</el-button>
                            </div>
                        </div>
                    </el-card>
                </div>
            </el-col>
        </el-row>
        <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList" />

        <!-- 添加或修改大屏管理对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="页面名称" prop="pageName">
                    <el-input v-model="form.pageName" placeholder="请输入页面名称" />
                </el-form-item>
                <el-form-item label="页面图片">
                    <image-upload v-model="form.pageImage" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listBEchartZt, getBEchartZt, delBEchartZt, addBEchartZt, updateBEchartZt } from '@/views/zutai/api/zutai/bEchartZt';
import { getToken } from '@/utils/auth';
import noImage from '../images/no-zt.png';

export default {
    name: 'BEchartZt',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 大屏管理表格数据
            bEchartZtList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderByColumn: 'id',
                isAsc: 'desc',
                guid: null,
                deptIdStrs: null,
                pageName: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            isAdmin: false,
            isType: false,
            baseApi: process.env.VUE_APP_BASE_API,
            noImage: noImage,
        };
    },
    created() {
        this.isAdmin = this.$auth.hasRole('admin');
        this.getList();
    },
    methods: {
        //预览
        handlePreview(row) {
            // window.open('http://' + this.tipUtil.getServerIp() + ':' + this.tipUtil.getServerPort() + '/screenFull?guid=' + row.guid)
            window.open('http://' + window.location.hostname + ':' + '85' + '/screenFull?guid=' + row.guid + '&accessToken=' + getToken());
        },
        //编辑
        handleClickZt(row) {
            if (this.isAdmin) {
                window.open('http://' + window.location.hostname + ':' + '85' + '/screenEditor?guid=' + row.guid + '&accessToken=' + getToken());
                // window.open('http://' + this.tipUtil.getServerIp() + ':' + this.tipUtil.getServerPort() + '/screenEditor?guid=' + row.guid +'&accessToken=' + getToken())
            } else {
                // window.open('http://' + this.tipUtil.getServerIp() + ':' + this.tipUtil.getServerPort() + '/screenEditor?guid=' + row.guid)
                window.open('http://' + window.location.hostname + ':' + '85' + '/screenEditor?guid=' + row.guid + '&accessToken=' + getToken());
            }
        },
        /** 查询大屏管理列表 */
        getList() {
            this.loading = true;
            listBEchartZt(this.queryParams).then((response) => {
                this.bEchartZtList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                guid: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                deptIdStrs: null,
                pageName: null,
                pageImage: null,
                pageData: null,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加大屏管理';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getBEchartZt(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = '修改大屏管理';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBEchartZt(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addBEchartZt(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除大屏管理编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delBEchartZt(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'ghxx/bEchartZt/export',
                {
                    ...this.queryParams,
                },
                `大屏_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
<style scoped>
.ztImage {
    width: 100%;
    display: block;
    height: 200px;
}

.ztImage:hover {
    cursor: pointer;
    transform: scale(1.1);
}

.device-name {
    background-color: #5479f7;
    display: inline-block;
    color: #fff;
    text-align: center;
    height: 25px;
    line-height: 15px;
    font-size: 12px;
    padding: 4px 6px;
    position: fixed;
    margin: -5px;
    z-index: 2;
}

.bottom {
    margin-top: 13px;
    line-height: 12px;
    text-align: right;
}

.clearfix:before,
.clearfix:after {
    display: table;
    content: '';
}

.clearfix:after {
    clear: both;
}
</style>
